package com.gujiangbo.defrewlock;

public class ReadWriteLockTest {

    private final static String text = "abcdefghijklmnopqrstuvwxyz";

    public static void main(String[] args) {
        ShareData shareData = new ShareData(50);
        // 创建2个线程写入
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                for (int j = 0; j < text.length(); j++) {
                    try {
                        char c = text.charAt(j);
                        shareData.write(c);
                        System.out.println(Thread.currentThread() + " write " + c);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        // 创建10个线程进行数据读操作
        for (int i = 0; i < 10; i++) {

            new Thread(() -> {
                while (true) {
                    try {
                        System.out.println(Thread.currentThread() + " read " + new String(shareData.read()));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        }

    }
}
